wifi.setmode(wifi.STATION)

if (wifi.sta.sethostname("NodeMCU-CLOCK") == true) then
    print("hostname was successfully changed")
else
    print("hostname was not changed")
end

wifi.sta.config("TP-LINK_S00", "shuang00")

dateutil = require("dateutil")

rsec, rusec = rtctime.get()
print('rtc', rsec, rusec)

cli = net.createConnection(net.TCP, 0)

tmr.alarm(1, 1000, 1, function()
    if (wifi.sta.getip() ~= nil) then
        tmr.stop(1)
        print("Config done, IP is " .. wifi.sta.getip())
        cli:dns(dateutil.sync_server, function(conn, ip) dateutil.synctime(ip) end)
    end
end )

tmr.alarm(2, 1000, 1, function()
    rsec, rusec = rtctime.get()
    --year, month, date, hour, minite, secend = dateutil.analyse(rsec, 8)
    --print(year .. "-" .. month .. "-" .. date .. " " .. hour .. ":" .. minite .. ":" .. secend)
    print(dateutil.format(dateutil.analyse(rsec, 8)))
end )

 
-- rsec, rusec = rtctime.get();year,month,date,hour,minite,secend=dateutil.getdate(rsec,8);print(year.."-"..month.."-"..date.." "..hour..":"..minite..":"..secend)
 
